package com.qigao.mall.channel.alipay;

import com.alibaba.fastjson.JSONObject;
import com.qigao.mall.channel.ChannelCore;
import com.qigao.mall.channel.config.ChannelConstant;
import com.qigao.mall.channel.dto.ChannelRefundRespDto;
import com.qigao.mall.commons.dto.MerChannelDto;
import com.qigao.mall.commons.enums.*;
import com.qigao.mall.commons.param.ChannelReqParam;
import com.qigao.mall.commons.param.ChannelRespParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author:luiz
 * @Date: 2018/5/28 11:16
 * @Descripton:支付宝退款查询接口
 * @Modify :
 **/
@Service
public class AliPayRefundQuery extends AbstractAliPay implements ChannelCore {
    private static final Logger logger= LoggerFactory.getLogger(AliPayRefundQuery.class);
    @Autowired
    ChannelConstant channelConstant;

    @Override
    public boolean isSupport(PayProductEnum payProductEnum, ChannelActionEnum channelActionEnum, ChannelInfoEnum channelInfoEnum) {
        return ChannelActionEnum.REFUND_QUERY.equals(channelActionEnum) &&
                ChannelInfoEnum.ALIPAY.equals(channelInfoEnum);
    }

    @Override
    protected String getTradeMethod() {
        return "alipay.trade.fastpay.refund.query";
    }

    @Override
    public Map buildSelfReqParam(MerChannelDto merChannelDto, ChannelReqParam channelReqParam) {
        Map reqParam=new ConcurrentHashMap<>();
        JSONObject bizContent=new JSONObject();
        bizContent.put("out_trade_no", merChannelDto.getInnerSeqNo());
        bizContent.put("out_request_no", merChannelDto.getInnerRefundSeqNo());
        reqParam.put("biz_content",bizContent.toString());
        return reqParam;
    }

    @Override
    protected ChannelRespParam buildSelfRespData(JSONObject respJson, MerChannelDto merChannelDto) {
        String code=respJson.getString("code");
        String subCode=respJson.getString("sub_code");
        String subMsg=respJson.getString("sub_msg");
        ChannelRespParam channelRespParam=new ChannelRespParam();
        channelRespParam.setRespCode(ChannelCodeEnum.SUCCESS.getCode());
        channelRespParam.setRespMsg(ChannelCodeEnum.SUCCESS.getName());
        String tradeNo=respJson.getString("trade_no");
        if(StringUtils.isEmpty(subCode)&&StringUtils.isEmpty(tradeNo)){
            channelRespParam.setRespCode(ChannelCodeEnum.RESP_NULL.getCode());
            channelRespParam.setRespMsg(ChannelCodeEnum.RESP_NULL.getName());
            return channelRespParam;
        }
        ChannelRefundRespDto channelRefundRespDto =new ChannelRefundRespDto();
        if("40004".equals(code) && "ACQ.TRADE_HAS_CLOSE".equals(subCode)){
            channelRespParam.setRespMsg(subMsg);
            channelRefundRespDto.setRefundStatus(RefundStatusEnum.REFUND_CLOSE);
            channelRespParam.setRespContent(JSONObject.toJSONString(channelRefundRespDto));
            return channelRespParam;
        }
        channelRefundRespDto.setThirdTradeNo(respJson.getString("trade_no"));
        channelRefundRespDto.setInnerSeqNo(respJson.getString("out_trade_no"));
        channelRefundRespDto.setInnerRefundNo(respJson.getString("out_request_no"));
        BigDecimal refundAmt=new BigDecimal(respJson.getString("refund_amount"));
        channelRefundRespDto.setRefundAmt(refundAmt);
        channelRefundRespDto.setRefundStatus(RefundStatusEnum.REFUND_SUCCESS);
        channelRespParam.setRespContent(JSONObject.toJSONString(channelRefundRespDto));
        return channelRespParam;
    }


}
